// ITI1120 (Fall 2012), Lab 3, Exercice 4 // Name: Grace Hopper, Student Number: 1234567 // Description: Program For Computing Number of Real Roots of a // quadratic equation import java.util.Scanner ; class Lab3Ex4 { /** * Given three coefficients of a quadratic equation, * computes number of real roots of the given equation */ public static void main (String[] args) { //Configuration for obtaining keyboard input //You can delete these lines if you use //the class IIT1120 to make the entry Scanner keyboard = new Scanner(System.in); // Declaration variable to read inputs into double a, b, c; // The coefficients int numberRoots; // Number of Reals // Print Identification information System.out.println(); System.out.println("ITI1120 (F-2012), Lab 3, Exercice 4"); System.out.println("Name: Grace Hopper, Student #: 1234567"); System.out.println("Assistant to the teaching") ; System.out.println(); // Read The Input Values System.out.println("Give the values of the coefficients a, b, and c."); a = keyboard.nextDouble(); b = keyboard.nextDouble(); c = keyboard.nextDouble(); // Call to the method of solve the problem numberRoots = howManyRoots(a, b, c); // Display results to the screen System.out.println("The number of real roots for the equation is "+numberRoots); } /** * Description: Determines the number of roots of a quadratic * equation. * Parameters (Input): a, b, c - Coefficients of equation **/ public static int howManyRoots(double a, double b, double c) { // Declaration of variable for result int nbrR; // A REAL NUMBER nbrR = 0; // Declaration of intermediate variables double discriminant; // b*b - 4*a*c // Calculate discriminant discriminant = Math.pow(b,2) - 4*a*c; if(discriminant < 0.0) { nbrR = 0; } else { if(discriminant > 0.0) { nbrR = 2; } else { nbrR = 1; } } // Return the result return(nbrR); } }